package network;

import java.io.IOException;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import network.CertificateForCA;
import encryption.RSA;

public class CAServer 
{
	ServerConnection connection;
	CertificateForCA certificate;
	private String CAPrivateMod = "16660929389029777513491376010661864846386518598590828747114307280212346276797736862264273823781716782895220228102793235254065972422358627420236596770567580972450734623153289998436812143618647104958164332163906411132834721217918660848114896205518354121058567144077021708810032950671970787962194347932218623873721392135829367566605455365112177258453999280706768896215609614447030039863576705311382314285644568552745113041949744691640354243517032822567748427033282656720035062158879560768410783399411759001912667180312290156520697932472386497579388628055886825296655161688083527680248008372064735780109946479620258696883";
	private String CAPrivateExp = "8118825105940994776551575206013356054354026854518771180674161424857126366721279955342353919713946429933346860623629472833267480892937502865445106649142412163147331445214511945772281804455576452140682913651624504416409198724620448797555538477797796011567914312727829551132294922145659079365253806179155804690828930471149683402693061733992632478819236791140278217152842178961290911652000255311266360975470994919725737880836720257106934954982357033613974462947333975778137189366930118862726754934459742025520625767296052843003171779630717928686202105468788829246457134730971638334220652147429470947345840824498267634177";
	
	
	public CAServer(ServerConnection connection)
	{
		this.connection = connection;
		this.certificate = new CertificateForCA("1234", "19037192548401257122813412906623969839721885538107749353520972030888318802242192898276384842722426377656425042129340128868382780642365131402342239403389530257365555425531885567837332194139302675246051066343236368638095411073600564979354690446307263012377302939006232964133061817032459213357588137384212950686798640147991239355756029781494233674312158380128385143452361994172908792172466064728248887230532774767631092956546260682663989747064693269836567085525744639453408238297003078635843534948582711664176726486635833116575975321051684361545431083598788603725214215435416034761656696281471144294832218311449669856673" , "65537", "ourCA.org");
		
		//Private Key: Private: 19037192548401257122813412906623969839721885538107749353520972030888318802242192898276384842722426377656425042129340128868382780642365131402342239403389530257365555425531885567837332194139302675246051066343236368638095411073600564979354690446307263012377302939006232964133061817032459213357588137384212950686798640147991239355756029781494233674312158380128385143452361994172908792172466064728248887230532774767631092956546260682663989747064693269836567085525744639453408238297003078635843534948582711664176726486635833116575975321051684361545431083598788603725214215435416034761656696281471144294832218311449669856673 673042329289496204488436725889920779386233864714522411036637200292479586566293253357742705350990462136349494523912920618704592867362863870168408207541595459150037260188250589143218314750763145986924948055560655296007859185765788929263848173765871620606347725859856901870642602347745670344225883307432769378224090697239666279360211023522081123390227755195914216176851874494946129140214888591940771142812085415917743129806798208577434405563809615887714745320224133275565891383166551500150921932311854547665365563995232794687139088351161150349070969285695691588171899208073079995115293967303247921536847524886730147073

	}
	
	private byte[] EncryptHashedCertificate(String hashedCertificate) 
	{
		//Encrypt with RSA
		RSA rsa = new RSA();
		
		// Waiting for method
		byte[] EncryptedHashedCertificate = new RSA().rsaEncryptToBytes(hashedCertificate, CAPrivateMod, CAPrivateExp);
		
		return EncryptedHashedCertificate;
	}
	
	public void CAServerFlow() throws NoSuchAlgorithmException, IOException
	{
		String hashedCertificate = CertificateForCA.HashedCertificate(certificate.toString());
		
		byte[] certificateInBytes = certificate.toString().getBytes("ISO-8859-1");
		byte[] encryptedHash = EncryptHashedCertificate(hashedCertificate);
		
		connection.sendMessage(certificate.toString());
		connection.receiveMessage();
	//	connection.sendRSAMessage(certificateInBytes);
		connection.sendRSAMessage(encryptedHash);
	}

}
